package com.cbw.cj01.action;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.cbw.cj01.util.CJUtil;
import com.ssh.web.common.BaseAction;

public class CaijiAction extends BaseAction {
	private static final long serialVersionUID = -2746785081655075016L;
	public String show() {
		return SUCCESS;
	}
	public void cj() throws Exception {
		String url = getRequest().getParameter("url");
		String encode = getRequest().getParameter("encode");
		String result = "No Result";
		if (StringUtils.isNotBlank(url)) {
			result = CJUtil.getRemoteContents(url,encode);
		}
		StringBuffer buff = new StringBuffer();
		String regex = getRequest().getParameter("regex");//"(?<=<h3 class=\"t\">).*(?=</h3>)";
		Pattern pattern = Pattern.compile(regex,Pattern.CANON_EQ);
		Matcher m = pattern.matcher(result);
//		String urlRegex = "<a target=\"_blank\" href=\".*?\"";
//		Pattern urlP = Pattern.compile(urlRegex, Pattern.CANON_EQ);
		while (m.find()) {
//			Matcher urlm = urlP.matcher(m.group());
//			while (urlm.find()) {
//				buff.append(urlm.group()).append("<br/>");
//			}
			buff.append(m.group()).append("<br/>");
		}
		getResponse().setContentType("text/html;charset="+encode);
		getResponse().getWriter().write(buff.toString());
	}
}
